#include "Event.h"
#include "../common/Constantes.h"

Event::Event() {}

Event::Event(SDL_Event event){
	this->event = event;
}

Event::Event(DTOShipStatus* dtoShip){
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = Ship_Status;
	event.user.data1 = new std::string(dtoShip->getOwner());

	std::string data2;
	data2 += UtilString::TOlenghtStringStringValue(dtoShip->posX);
	data2 += UtilString::TOlenghtStringStringValue(dtoShip->posY);
	data2 += UtilString::TOlenghtStringStringValue(dtoShip->angle);

	event.user.data2 = new std::string(data2);

	this->setEvent(event);
}

Event::Event(DTOCliente* dtoClient) {
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = NewUser;
	event.user.data1 = new std::string(dtoClient->getOwner());

	this->setEvent(event);
}

Event::Event(DTOEvent* dtoEvent) {
	std::stringstream ss;
	ss << "tipo de evento: " << dtoEvent->type;
	Logger::log("Event", "constructor", &ss);

	// Armo un SDL_Event
	SDL_Event event;
	switch (dtoEvent->type) {
	//Por ahora, sòlo se envian al servidor eventos de teclado asì que ni miro como levantar
	//otro tipo de eventos desde un DTO
	case KeyPressed:
		event.type = SDL_USEREVENT;
		event.user.code = UserKeyPressed;
		event.user.data1 = new std::string(dtoEvent->getOwner()); //TODO fijarse de borrarlo luego
		event.user.data2 = (void*) dtoEvent->key;

		this->sender = dtoEvent->getOwner();

		break;

	case KeyReleased:
		event.type = SDL_USEREVENT;
		event.user.code = UserKeyReleased;
		event.user.data1 = new std::string(dtoEvent->getOwner());
		event.user.data2 = (void*) dtoEvent->key;

		this->sender = dtoEvent->getOwner();

		break;

	default:
		break;
	}
	setEvent(event);
}

Event::~Event() {
	// TODO Auto-generated destructor stub
}

void Event::setEvent(SDL_Event event) {
	this->event = event;
}

void Event::setSender(std::string sender) {
	this->sender = sender;
}

EventType Event::getType() {
	return (EventType) this->event.type;
}

EventKey Event::getKey() {
	return (EventKey) this->event.key.keysym.sym;
}

EventButtonState Event::getButtonState() {
	return (EventButtonState) this->event.button.state;
}

WindowID Event::getCode() {
	return (WindowID) this->event.user.code;
}

std::string Event::getData() {
	std::string* data;
	data = (std::string*) this->event.user.data1;
	return *data;
}

std::string Event::getData2() {
	std::string* data;
	data = (std::string*) this->event.user.data2;
	return *data;
}

int Event::getintData2() {
//	int* data;
//	data = (int*) this->event.user.data2;
//	return *data;
	return 1;
}

Uint16 Event::getKeyUnicode() {
	return (Uint16) this->event.key.keysym.unicode;
}

Uint16 Event::getXCoordinate() {
	return (Uint16) this->event.button.x;
}

Uint16 Event::getYCoordinate() {
	return (Uint16) this->event.button.y;
}
SDL_Event* Event::getEvent() {
	return &(this->event);

}

Uint16 Event::getW() {
	return (Uint16) (this->event.resize.w);
}

Uint16 Event::getH() {
	return (Uint16) (this->event.resize.h);
}

/*Maneje sus punteros con responsabilidad..*/
void Event::eraseData() {
	delete (std::string*) this->event.user.data1;
}

DTOEvent* Event::transformInDTO() {
	return new DTOEvent(getKeyUnicode(), getType(), getH(), getW(),
			getXCoordinate(), getYCoordinate(), getCode(), getButtonState(),
			getKey(), sender);
}

DTOShipStatus* Event::transformInDTOShipStatus(){
	std::string data2 = getData2();

	int posX = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(data2));
	int posY = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(data2));
	int angle = UtilString::stringToInt(UtilString::FROMlenghtStringStringValue(data2));

	//TODO ADD THIS
	//return new DTOShipStatus(this->sender,posX,posY,angle);
	return NULL;
}

std::string Event::getSender() {
	return sender;
}
